/**
 * 
 */
package neptune.console.cli.commands;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import neptune.console.CLCommand;
import neptune.console.NeptuneCLI;

import org.springframework.stereotype.Component;

/**
 * @author Roberto Bifulco [info@robertobifulco.it]
 * 
 */
@Component
public class CreateExpTopology implements CLCommand {

	@Override
	public String getCommandHelp() {
		return "Sets the given topology file as the experiment's topology";
	}

	@Override
	public String getCommandName() {
		return "uploadExpTopology";
	}

	@Override
	public void processCommand(List<String> args) throws Exception {
		copyFile(args.remove(0), NeptuneCLI.getConf()
				.getTopologyRootDirectoryPath()
				+ File.separator + args.remove(0) + ".topology.xml");
	}

	private void copyFile(String pathSorgente, String pathDestinazione) {
		File fileSorgente = new File(pathSorgente);
		File fileDestinazione = new File(pathDestinazione);
		try {
			BufferedInputStream bis = new BufferedInputStream(
					new FileInputStream(fileSorgente));
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(fileDestinazione));
			int i = 0;
			while ((i = bis.read()) != -1) {
				bos.write(i);
				bos.flush();
			}
			bos.close();
			bis.close();
		} catch (FileNotFoundException fnfe) {
			System.err.println(fnfe);
		} catch (IOException ioe) {
			System.err.println(ioe);
		}
	}

	/* (non-Javadoc)
	 * @see neptune.console.CLCommand#getParamsDescription()
	 */
	@Override
	public String getParamsDescription() {
		return "<path_to_topology_file> <experiment_name>";
	}
}
